package com.sddz.pacs.exception;

import com.sddz.pacs.dto.JsonResult;
import org.apache.log4j.Logger;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2018/5/15/015.
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    private static Logger logger = Logger.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public JsonResult jsonErrorHandler(HttpServletRequest req, Exception e) {
        logger.error("error:",e);
        if(e instanceof CustomerException){
            return JsonResult.failure(e.getMessage());
        }else if(e instanceof UnauthorizedException || e instanceof UnauthenticatedException){
            return JsonResult.failure("您没有权限操作，请联系管理员！");
        }else {
            return JsonResult.failure("系统异常，请联系技术人员！");
        }
    }
}
